/*
* BankService
*
* r1
*
* GNU General Public License v3.0
*/

package com.acme.bankapp.service.bank;

import com.acme.bankapp.domain.bank.*;

public class BankService {

	private static class InterestAssociation {
		private Gender gender;
		private int age;
		private double interest;
		
		public InterestAssociation(Gender gender, int age, double interest) {
			this.gender = gender;
			this.age = age;
			this.interest = interest;
		}
		
		public Gender getGender() {
			return this.gender;
		}
		
		public int getAge() {
			return this.age;
		}
		
		public double getInterest() {
			return this.interest;
		}
	}
	
	private static InterestAssociation[] interestTable = {
			new InterestAssociation(Gender.MALE, 1, 5),
			new InterestAssociation(Gender.FEMALE, 1, 4),
			new InterestAssociation(Gender.MALE, 18, 7),
			new InterestAssociation(Gender.FEMALE, 18, 7.2),
			new InterestAssociation(Gender.MALE, 50, 10),
			new InterestAssociation(Gender.FEMALE, 60, 10),
	};
	
	public void addClient(Bank bank, Client client) {
		bank.getClients().add(client);
	}
	
	public void printMaximumAmountToWithdraw(Bank bank) {
		int i;
		
		for(Client client : bank.getClients()) {
			System.out.println(client.getClientSalutation() + "'s accounts:");
			i = 1;
			for(AbstractAccount a : client.getAccounts()) {
				System.out.println("	#" + i + " maximum amount to withdraw: " + a.maximumAmountToWithdraw());
				i++;
			}
		}
	}
	
	public void calculateInterests(Bank bank) {
		SavingsAccount savingsAccount;
		int usedAge;
		double interest;
		
		for(Client client : bank.getClients()) {
			for(AbstractAccount abstractAccount : client.getAccounts()) {
				if(abstractAccount instanceof SavingsAccount) {
					savingsAccount = (SavingsAccount)abstractAccount;
					usedAge = 0;
					interest = 0;
					for(InterestAssociation association : BankService.interestTable) {
						if(client.getGender() == association.getGender() && client.getAge() >= association.getAge() && association.getAge() > usedAge) {
							interest = association.getInterest();
							usedAge = association.getAge();
						}
					}
					savingsAccount.setInterest(interest);
				}
			}
		}
	}
	
}
